### REPLICATION FILE -- ANALYSIS, US
### Jonathan Homola & Margit Tavits
### "Contact Reduces Immigration-Related Fears for Leftist
### but Not for Rightist Voters"
### Comparative Political Studies

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
# install.packages("stargazer"); install.packages("foreign")
library("foreign"); library("stargazer")

## set working directory and source interaction functions
setwd(" ... ")
source("HomolaTavits_ContactFears_interactions.R")

## read in the main dataset 
data <- read.dta("HomolaTavits_ContactFears_US.dta")

## create contact factor score
contact <- prcomp(~ as.numeric(contact1) + as.numeric(contact2), 
                  data=data, na.action = na.exclude)
data$contact <- -contact$x[,1]

## Eigenvalue of first and second factor
(contact$sdev^2*2/sum(contact$sdev^2))[1]  ## 1.56
(contact$sdev^2*2/sum(contact$sdev^2))[2]  ## 0.44

## recode outcome variables
data$violence_com <- 5-as.numeric(data$violence_com)
data$violence_nat <- 5-as.numeric(data$violence_nat)
data$econ_hh <- 5-as.numeric(data$econ_hh)
data$econ_nat <- 5-as.numeric(data$econ_nat)
data$nat_id <- 5-as.numeric(data$nat_id)
data$amer_cult <- 5-as.numeric(data$amer_cult)

## create threat factor score
threat <- prcomp(~ violence_com + violence_nat + econ_hh + econ_nat + nat_id + amer_cult,
                 data=data, na.action = na.exclude)
data$threat <- threat$x[,1]

## Eigenvalue of first and second factor
(threat$sdev^2*6/sum(threat$sdev^2))[1]  ## 4.58
(threat$sdev^2*6/sum(threat$sdev^2))[2]  ## 0.56



#### Table 2: The Effect of Contact and Left-Right Affinity on Immigration-Related
#### Threat, the U.S.

data$PID3 <- relevel(as.factor(data$PID3), ref=3)
data2 <- subset(data, PID3!=3)
moda <- lm(threat ~ contact + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data)
mod0 <- lm(threat ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod1 <- lm(violence_com ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod2 <- lm(violence_nat ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod3 <- lm(econ_hh ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod4 <- lm(econ_nat ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod5 <- lm(nat_id ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)
mod6 <- lm(amer_cult ~ contact*PID3 + female + age + as.numeric(education) + as.numeric(income) + as.factor(region), data2)

stargazer(moda, mod0, mod1, mod2, mod3, mod4, mod5, mod6,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.05, 0.01, NA), 
          column.sep.width="1pt", font.size="small", digits=2, 
          dep.var.caption="Outcome variable:",
          dep.var.labels = c("Threat Factor", "Violence Com", "Violence Nat",
                             "Econ HH", "Econ Nat",
                             "Identity", "Culture"))




#### Figure 1: Marginal Effect of Contact on Threat Perceptions, the U.S.

#pdf("Fig1.pdf", height=8,width=7)
layout(matrix(c(0,0,0,1,1,1,1,1,0,0,0,
                2,2,2,2,2,0,3,3,3,3,3,
                4,4,4,4,4,0,5,5,5,5,5,
                6,6,6,6,6,0,7,7,7,7,7), 4, 11, byrow = TRUE))
interaction_plot_binary(mod0, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.1, ymin=-0.75,
                        factor_labels=c("Dem", "Rep"),
                        title="Threat (score)")
interaction_plot_binary(mod1, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="Violence, community")
interaction_plot_binary(mod2, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="Violence, national")
interaction_plot_binary(mod3, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="Economy, household")
interaction_plot_binary(mod4, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="Economy, national")
interaction_plot_binary(mod5, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="National identity")
interaction_plot_binary(mod6, "contact", "PID3", "contact:PID32",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.15, ymin=-0.4,
                        factor_labels=c("Dem", "Rep"),
                        title="American culture")
par(mfrow=c(1,1))
#dev.off()
